<template>
  <!--注册-->
  <div class="wrap">
    <div v-if="step === 1" class="tdbModule register">
      <div class="registerTitle">注册账户</div>
      <div class="registerLc1">
        <p class="p1">填写账户信息</p>
        <p class="p2">注册成功</p>
      </div>

      <div class="registerCont">
        <ul>
          <li>
            <span class="dis"></span>
            <input v-model="userInfo.userType" type="radio" value="1" />
            我要投资
            <input v-model="userInfo.userType" type="radio" value="2" />
            我要借钱
          </li>
          <li class="telNumber">
            <span class="dis">手机号码</span>
            <input class="input" v-model="userInfo.mobile" />
            <button v-if="!sending" class="button" @click="send()">
              获取验证码
            </button>
            <button v-else disabled class="button disabled">
              {{ leftSecond }}秒后重发
            </button>
          </li>

          <li>
            <span class="dis">短信验证码</span>
            <input class="input" v-model="userInfo.code" />
            <span class="info">
              请输入验证码
            </span>
          </li>

          <li>
            <span class="dis">密码</span>
            <input type="password" v-model="userInfo.password" class="input" />
            <span class="info">
              6-24个字符，英文、数字组成，区分大小写
            </span>
          </li>

          <li class="agree">
            <input type="checkbox" checked />
            我同意《<NuxtLink to="#" target="_black">尚融宝注册协议</NuxtLink>》
            <span>请查看协议</span>
          </li>
          <li class="btn">
            <button @click="register()">
              下一步
            </button>
          </li>
        </ul>
      </div>
    </div>

    <div v-if="step === 2" class="tdbModule register">
      <div class="registerTitle">注册账户</div>
      <div class="registerLc2">
        <p class="p1">填写账户信息</p>
        <p class="p2">注册成功</p>
      </div>
      <div class="registerCont">
        <ul>
          <li class="scses">
            {{ this.userInfo.mobile }} 恭喜您注册成功！
            <NuxtLink class="blue" to="/login">
              请登录
            </NuxtLink>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import "~/assets/css/register.css";
export default {
  data() {
    return {
      timer: {},
      step: 1, //注册步骤
      userInfo: {
        userType: 1,
      },
      flag: false, //还未提交注册请求
      sending: false, // 是否发送验证码
      second: 10, // 倒计时间
      leftSecond: 0, //剩余时间
    };
  },

  methods: {
    //发短信
    send() {
      //设置发送验证码按钮隐藏，显示倒计时按钮 并且每一秒更新一次倒计时 直到倒计时为0 再显示发送验证码按钮隐藏倒计时按钮
      this.timeDown();
      //发送验证码
      this.$axios
        .$get(`/admin/sms/send/${this.userInfo.mobile}/1`)
        .then((r) => {
          this.$message.success(r.message);
          console.log(r);
        });
    },

    //倒计时:
    timeDown() {
      this.sending = true;
      //初始化倒计时
      this.leftSecond = 10;
      //定时任务 每过一秒 leftsecond--
      // setTimeout() 延迟任务  过指定时长后执行一次
      this.timer = setInterval(() => {
        //setInterval执行后返回定时任务对象
        this.leftSecond--;
        //如果leftSecond<=0 停止定时任务
        if (this.leftSecond <= 0) {
          clearInterval(this.timer);
          //切换显示获取验证码按钮
          this.sending = false;
        }
      }, 1000); //每过1000ms执行一次匿名函数
    },

    //注册
    register() {
      //判断是否已经提交注册请求了
      if (this.flag) {
        //flag=true方法结束
        this.$message.warning("正在处理注册请求，请稍后...");
        return;
      }
      //flag = false 执行当前行  false代表第一次注册
      this.flag = true;
      //提交注册数据到后端保存
      this.$axios({
        url: "/api/core/userInfo/register",
        method: "POST",
        data: this.userInfo,
      }).then((r) => {
        this.flag = false;
        //注册成功后跳转到注册成功页
        this.$message.success(r.data.message);
        //console.log("注册结果:", r);
        //修改步骤条的值为2
        this.step = 2;
      });
    },
  },
  destroyed() {
    if (this.timer) clearInterval(this.timer);
  },
};
</script>
